package com.ruoyi.cm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.cm.domain.bo.CmBsProcessInspectionInfoBo;
import com.ruoyi.cm.domain.vo.CmBsProcessInspectionInfoVo;
import com.ruoyi.cm.domain.CmBsProcessInspectionInfo;
import com.ruoyi.cm.mapper.CmBsProcessInspectionInfoMapper;
import com.ruoyi.cm.service.ICmBsProcessInspectionInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工序报验资料(工序报验的子表)Service业务层处理
 *
 * @author zxs
 * @date 2024-10-22
 */
@RequiredArgsConstructor
@Service
public class CmBsProcessInspectionInfoServiceImpl implements ICmBsProcessInspectionInfoService {

    private final CmBsProcessInspectionInfoMapper baseMapper;

    /**
     * 查询工序报验资料(工序报验的子表)
     */
    @Override
    public CmBsProcessInspectionInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工序报验资料(工序报验的子表)列表
     */
    @Override
    public TableDataInfo<CmBsProcessInspectionInfoVo> queryPageList(CmBsProcessInspectionInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CmBsProcessInspectionInfo> lqw = buildQueryWrapper(bo);
        Page<CmBsProcessInspectionInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工序报验资料(工序报验的子表)列表
     */
    @Override
    public List<CmBsProcessInspectionInfoVo> queryList(CmBsProcessInspectionInfoBo bo) {
        LambdaQueryWrapper<CmBsProcessInspectionInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CmBsProcessInspectionInfo> buildQueryWrapper(CmBsProcessInspectionInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CmBsProcessInspectionInfo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), CmBsProcessInspectionInfo::getName, bo.getName());
        lqw.eq(bo.getProcessInspectionId() != null, CmBsProcessInspectionInfo::getProcessInspectionId, bo.getProcessInspectionId());
        lqw.eq(StringUtils.isNotBlank(bo.getFileType()), CmBsProcessInspectionInfo::getFileType, bo.getFileType());
        lqw.eq(StringUtils.isNotBlank(bo.getFileNo()), CmBsProcessInspectionInfo::getFileNo, bo.getFileNo());
        lqw.like(StringUtils.isNotBlank(bo.getFileName()), CmBsProcessInspectionInfo::getFileName, bo.getFileName());
        return lqw;
    }

    /**
     * 新增工序报验资料(工序报验的子表)
     */
    @Override
    public Boolean insertByBo(CmBsProcessInspectionInfoBo bo) {
        CmBsProcessInspectionInfo add = BeanUtil.toBean(bo, CmBsProcessInspectionInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工序报验资料(工序报验的子表)
     */
    @Override
    public Boolean updateByBo(CmBsProcessInspectionInfoBo bo) {
        CmBsProcessInspectionInfo update = BeanUtil.toBean(bo, CmBsProcessInspectionInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CmBsProcessInspectionInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工序报验资料(工序报验的子表)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
